class_name IgorBoss
extends BasicEnemy



const STATE_SPRINT := "sprint"
const STATE_KICK := "kick"
const STATE_DEFENSE := "defense"
const STATE_RECOVER := "recover"


const ANIMATION_PRE_SPRINT := "pre_sprint"
const ANIMATION_SPRINT := "sprint"
const ANIMATION_KICK := "kick"
const ANIMATION_DEFENSE := "defense"
const ANIMATION_RECOVER := "recover"


# 冲刺伤害触发器
@onready var sprintDamageEmitter: Area2D = $SprintDamageEmitter


## 冲刺伤害
@export var damage_sprint: float
## 冲刺速度
@export var sprint_speed: float
## 冲刺准备时间
@export var duration_pre_sprint: float
## 冲刺检测距离，特指 x 轴的距离，超过此距离才会进行冲刺
@export var sprint_detection_distance: float = 35.0
## 冲刺撞墙时的击退强度
@export var sprint_knockback_intensity: float
## 防御受击时的击退强度
@export var defense_knockback_intensity: float
## 防御受击后退时反向的减速度
@export var defense_knockback_deceleration: float
## 冲刺结束后恢复到正常状态的时间
@export var duration_recover: float
## 近战攻击时，使用脚踢的概率百分比
@export_range(0, 100, 1) var kick_rate: int = 30




func _ready():
	super._ready()
	sprintDamageEmitter.area_entered.connect(on_emit_sprint_damage)



func _process(_delta: float):
	if not enabled:
		return



func is_player_within_sprint_range() -> bool:
	var target := projectileAim.get_collider()
	if target == null:
		return false
	return target is Player and absf(global_position.x - target.global_position.x) > sprint_detection_distance


# 计算下次攻击状态
func next_attack_state() -> String:
	var i := randi_range(0, 100)
	if i < kick_rate:
		return STATE_KICK
	return STATE_PUNCH


# 设置当前生命值
func set_current_health(health: int):
	super.set_current_health(health)
	DamageManager.enemy_health_change.emit(enemy_type, current_health, max_health)



# 发射伤害
func _emit_damage(damage_receiver: DamageReceiver):
	var hitType := DamageReceiver.HitType.NORMAL
	match stateMachine.current_node_state_name:
		STATE_PUNCH:
			hitType = DamageReceiver.HitType.NORMAL
			SoundPlayer.play(SoundManager.Sound.HIT1, true)
		STATE_KICK:
			hitType = DamageReceiver.HitType.KNOCK_DOWN
			SoundPlayer.play(SoundManager.Sound.HIT2, true)
	damage_receiver.damage_received.emit(damage, damage_receiver.global_position - damageEmitter.global_position, hitType)


# 受到伤害
func _receive_damage(damage_value: int, _emit_direction: Vector2, hit: DamageReceiver.HitType):
	# 下列状态不会受伤
	if stateMachine.current_node_state_name == STATE_SPRINT:
		return
	# 除了恢复状态以外，其他时候被攻击都会进入防御状态
	if stateMachine.current_node_state_name != STATE_HURT and stateMachine.current_node_state_name != STATE_RECOVER:
		stateMachine.transition_to(STATE_DEFENSE)
		return
	# 连击计数
	ComboManager.register_hit.emit()
	# 计算受伤
	set_current_health(current_health - damage_value)
	match hit:
		DamageReceiver.HitType.NORMAL:
			stateMachine.transition_to(STATE_HURT)
		DamageReceiver.HitType.SHOOT:
			stateMachine.transition_to(STATE_HURT)
		DamageReceiver.HitType.KNOCK_DOWN:
			stateMachine.transition_to(STATE_FALL)
		DamageReceiver.HitType.POWER:
			EntityManager.spawn_spark.emit(global_position)
			stateMachine.transition_to(STATE_FLY)


# 发送冲刺伤害
func on_emit_sprint_damage(receiver_area: Area2D):
	var damage_receiver: DamageReceiver = receiver_area as DamageReceiver
	if damage_receiver == null:
		return
	damage_receiver.damage_received.emit(damage_sprint,
										damage_receiver.global_position - sprintDamageEmitter.global_position,
										DamageReceiver.HitType.KNOCK_DOWN)
	SoundPlayer.play(SoundManager.Sound.HIT2, true)
